<template>
<div>
  <el-card>
    <el-row :gutter="25">
      <el-col :span="10">
        <!-- 搜索添加 -->
        <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @clear="getReleasedOrderList()">
          <el-button slot="append" icon="el-icon-search" @click="getReleasedOrderList()"></el-button>
        </el-input>
      </el-col>
      <el-button-group>
        <el-button type="primary" icon="el-icon-back" @click="backward">返回</el-button>
      </el-button-group>
    </el-row>
    <!-- 订单列表 -->
    <el-table :data="orderList" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column label="订单编号" prop="ord_code"></el-table-column>
      <el-table-column label="产品名称" prop="ord_pro_name" width="100"></el-table-column>
      <el-table-column label="产品数量" prop="ord_pro_num"></el-table-column>
      <el-table-column label="投标截止" prop="ord_ddl" width="100"></el-table-column>
      <el-table-column label="交货时间" prop="ord_del_date" width="100"></el-table-column>
      <el-table-column label="收货人" prop="ord_rece"></el-table-column>
      <el-table-column label="联系电话" prop="ord_rece_tel"></el-table-column>
      <el-table-column label="收货地址" prop="ord_loc"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!--购买-->
          <el-button type="success" icon="el-icon-check" @click="buyOrder(scope.row.ord_id)" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 100]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    ></el-pagination>
  </el-card>
  <!-- 创建订单对话框 -->
  <el-dialog title="添加新订单" :visible.sync="addDialogVisible" width="50%"
             @close="addDialogClosed">
    <!-- 内容主体区域 -->
    <el-form :model="addForm" :rules="addFormRules"
             ref="addFormRef" label-width="70px">
      <!-- 订单编号 -->
      <el-form-item label="订单ID" prop="bid_ord_id">
        <el-input v-model="addForm.bid_ord_id" disabled></el-input>
      </el-form-item>

      <!-- 工厂编号 -->
      <el-form-item label="工厂ID" prop="bid_fac_id">
        <el-input v-model="addForm.bid_fac_id" disabled></el-input>
      </el-form-item>

      <!-- 投标价格 -->
      <el-form-item label="价格" prop="bid_price">
        <el-input v-model="addForm.bid_price"></el-input>
      </el-form-item>
    </el-form>
    <!-- 内容底部区域 -->
    <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addBide">确 定</el-button>
      </span>
  </el-dialog>
</div>
</template>

<script>
export default {
  data(){
    return{
      fac_id: '',
      ord_id: '',
      // 请求数据
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5,
        fac :'',
        ord :'',
      },
      orderList: [],// 用户列表
      addDialogVisible: false ,// 对话框显示
      addForm:{
        bid_price:'',
        bid_ord_id:'',
        bid_fac_id:'',
      },
    }
  },
  created() {
    this.getTempFacId();
    this.getReleasedOrderList();
  },
  methods:{
    loadForm(){
      this.addForm.bid_ord_id = this.ord_id;
      this.addForm.bid_fac_id = this.fac_id;
      this.addDialogVisible = true;
    },
    getTempFacId(){
      this.fac_id = window.sessionStorage.getItem("fac_id");
    },
    async getReleasedOrderList() {
      // 调用post请求
      this.queryInfo.fac = this.fac_id;
      this.queryInfo.ord = 0;
      const { data: res } = await this.$http.get("order/releasedOrderList", {
        params: this.queryInfo
      });
      for(var i in res.data){
        if(res.data[i].ord_state === 0){
          res.data[i].ord_state = '未发布';
        }
        if(res.data[i].ord_state === 1){
          res.data[i].ord_state = '已发布';
        }
        if(res.data[i].ord_state === 2){
          res.data[i].ord_state = '已中标';
        }
        if(res.data[i].ord_state === 3){
          res.data[i].ord_state = '已排产';
        }
        if(res.data[i].ord_state === 4){
          res.data[i].ord_state = '已生产';
        }
        if(res.data[i].ord_state === 5){
          res.data[i].ord_state = '已发货';
        }
        if(res.data[i].ord_state === 6){
          res.data[i].ord_state = '已结束';
        }
      }
      this.orderList= res.data; // 将返回数据赋值

      //使用for循环更新对应的变量
      this.total = res.numbers; // 总个数
    },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getReleasedOrderList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getReleasedOrderList(); // 数据发生改变重新申请数据
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.addFormRef.resetFields();// 重置表单项
    },
    // 关闭窗口
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();
    },
    async buyOrder(id){
      this.queryInfo.ord = id;
      this.ord_id = id;
      this.queryInfo.fac = this.fac_id;
      const { data: res } = await this.$http.get("order/isInBide", {
        params: this.queryInfo
      });
      if(res === 'success'){
        this.loadForm();
      }else{
        this.$message.error("您已接单，无法重复接单");
      }
    },
    addBide(){
      this.$refs.addFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("myOrder/addBide",this.addForm);
        if (res !== "success") {
          return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getMyOrderList();
      })
    },
    backward(){
      this.$router.push("/myOrder");
    }
  },
}
</script>

<style scoped>

</style>